package bot.controller;

import bot.conf.PathConf;
import bot.pojo.BaiduImgUrlList;
import bot.pojo.ImgStrlist;
import bot.pojo.JsonReturn;
import bot.pojo.ShiTuReturnData;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/baidu")
/**
 * 百度识图
 * */
public class BaiDuImage {
    @Autowired
    PathConf PathConf;
    @PostMapping("/shitu")
    public JsonReturn shitu(MultipartFile image) throws IOException {
        ShiTuReturnData shiTuReturnData = JSONObject.parseObject(HttpHelp.SendPostByFrom(image, "https://graph.baidu.com/upload",PathConf), ShiTuReturnData.class);
        //获取相关图片10长
        String url = shiTuReturnData.getData().getUrl();
        String xiangGuanData = bot.pojo.HttpHelp.sendGet(url, null, null);
        String substring = "";
        try {
            substring = xiangGuanData.substring(xiangGuanData.indexOf("firstUrl"), xiangGuanData.lastIndexOf("moreUrl"));
        }catch (Exception e){
            return JsonReturn.ok("无结果捏,或者传入图片大于20m");
        }
        substring = substring.substring(substring.indexOf("https")).replaceAll("[\",\\\\]" , "");
        BaiduImgUrlList baiduImgUrlList = JSONObject.parseObject(bot.pojo.HttpHelp.sendGet(substring, null, null), BaiduImgUrlList.class);
        //第一条是搜索的数据，要跳过
        List<ImgStrlist> collect = baiduImgUrlList.getData().getList().stream().skip(1).limit(10).collect(Collectors.toList());
        return JsonReturn.ok(collect);
    }
}
